package org.cfx.gift.controller;

import cn.hutool.http.HttpStatus;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.cfx.gift.config.AliOssConfig;
import org.cfx.gift.service.FileService;
import org.cfx.gift.util.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author RTX 9090
 */
@RequestMapping("/common/oss")
@RestController
public class CommonController {

    @Autowired
    private FileService fileService;

    /**
     * 获取aliyun oss上传的签名
     * @return AjaxResult
     */
    @GetMapping("/sign")
    public AjaxResult getSign(){
        Map<String, String> sign = fileService.getSign();
        return new AjaxResult().setResultObj(sign);
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody Map<String, String> map){
        String objectNameUrl = map.get("filePath");
        System.out.println(objectNameUrl);
        Boolean delete = fileService.delete(objectNameUrl);
        if (delete){
            return new AjaxResult().setMessage("删除成功！");
        }
        return new AjaxResult().setMessage("删除失败，文件不存在").setSuccess(false).setCode(HttpStatus.HTTP_NOT_FOUND);
    }

}
